package com.uphone.mvpdemo;

import android.Manifest;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.PagerAdapter;

import com.blankj.utilcode.util.LogUtils;
import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;
import com.uphone.mvpdemo.base.BaseGActivity;
import com.uphone.mvpdemo.base.OnPermissionCallBack;
import com.uphone.mvpdemo.http.ApiService;
import com.uphone.mvpdemo.http.FailureConsumer;
import com.uphone.mvpdemo.http.RequestBodyUtil;
import com.uphone.mvpdemo.http.RxSchedulers;
import com.uphone.mvpdemo.http.SuccessfulConsumer;
import com.uphone.mvpdemo.pro.fragment.home.HomeFragment;
import com.uphone.mvpdemo.view.NoScrollViewPager;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class MainActivity extends BaseGActivity {
    @BindView(R.id.vp_main)
    NoScrollViewPager mVpMain;
    @BindView(R.id.bottom_navigation)
    BottomNavigationView bottomNavigation;
    private ViewPagerAdapter mViewPagerAdapter;
    List<Fragment> fragments = new ArrayList<>();

    @Override
    protected void initBundle() {

    }

    @Override
    protected int initLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        mVpMain.setNoScroll(true);
        // TODO: 2019/5/27 为fragments添加数据
        fragments.add(HomeFragment.newInstance("首页"));
        fragments.add(HomeFragment.newInstance("项目"));
        fragments.add(HomeFragment.newInstance("体系"));
        fragments.add(HomeFragment.newInstance("导航"));
        fragments.add(HomeFragment.newInstance("公众号"));

        //除去自带效果
        bottomNavigation.setItemIconTintList(null);


        mViewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), fragments);
        mVpMain.setAdapter(mViewPagerAdapter);
    }

    @Override
    protected void initTitle() {

    }

    @Override
    protected void initData() {

        getPermission(new OnPermissionCallBack() {
            @Override
            public void permissionPass(String[] permissions) {
                File file = new File("/storage/emulated/0/DCIM/Screenshots/Screenshot_2019-05-24-18-18-57-594_com.uphone.kingmall.png");
                Map<String, RequestBody> mapRB = new HashMap<>();
                mapRB.put("token", RequestBodyUtil.convertToRequestBody("d7f55d40ff168e4158b4833f37d51ae6"));
                mapRB.put("uid", RequestBodyUtil.convertToRequestBody("8"));
                MultipartBody.Part part = RequestBodyUtil.filesToMultipartBodyPart("file", file);
                MyApp.apiService(ApiService.class)
                        .userHightScoreUpload(mapRB, part)
                        .compose(RxSchedulers.io_main())
                        .doOnSubscribe(d -> showLoading())
                        .doFinally(() -> closeLoading())
                        .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from
                                (MainActivity.this)))
                        .subscribe(new SuccessfulConsumer() {
                            @Override
                            public void success(String jsonObject) {
                                LogUtils.e("bean:" + jsonObject);
                            }
                        }, new FailureConsumer());
            }

            @Override
            public void permissionRefuse(String[] permissions) {

            }
        }, Manifest.permission.WRITE_EXTERNAL_STORAGE);


    }

    @Override
    protected void initListener() {
        bottomNavigation.setOnNavigationItemSelectedListener(item -> {
            switch (item.getItemId()) {
                case R.id.action_home:
                    naviTab(0);
                    break;
                case R.id.action_project:
                    naviTab(1);
                    break;
                case R.id.action_system:
                    naviTab(2);
                    break;
                case R.id.action_navi:
                    naviTab(3);
                    break;
                case R.id.action_pub:
                    naviTab(4);
                    break;
                default:
                    break;
            }
            return true;
        });

    }

    /**
     * 点击Navigation切换Tab
     *
     * @param position tab下标
     */
    private void naviTab(int position) {
        mVpMain.setCurrentItem(position, true);
    }

    public class ViewPagerAdapter extends FragmentPagerAdapter {

        private List<Fragment> mFragments;
        private List<String> listTitle = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager fm, List<Fragment> mFragments) {
            super(fm);
            this.mFragments = mFragments;
        }

        public ViewPagerAdapter(FragmentManager fm, List<Fragment> mFragments, List<String> listTitle) {
            super(fm);
            this.mFragments = mFragments;
            this.listTitle = listTitle;
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return listTitle.get(position);
        }

        @Override
        public int getItemPosition(Object object) {
            return PagerAdapter.POSITION_NONE;
        }

        public void recreateItems(List<Fragment> fragmentList, List<String> titleList) {
            this.mFragments = fragmentList;
            this.listTitle = titleList;
            notifyDataSetChanged();
        }
    }
}
